<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>系统消息列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">系统消息列表</div>
                <div class="layui-card-body">


                    <div class="layui-form-item layui-form">
                        <div class="layui-btn-group test-table-operate-btn">
                            <button class="layui-btn" data-type="addadmin" id="send">批量发送</button>
                        </div>

                    </div>

                    <table class="layui-hide" id="test-table-operate" lay-filter="test-table-operate" lay-size="lg">


                    </table>

                    <script type="text/html" id="test-table-operate-barDemo">
                        <a class="layui-btn layui-bg-green layui-btn-xs" lay-event="edit">发送消息</a>

                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<!--添加单个发送-->
<div id="addpassmodal"  style="display: none;padding:20px;">
    <form class="layui-form" action="" lay-filter="component-form-group">
        <div class="layui-form-item">
            <label class="layui-form-label">消息标题</label>
            <div class="layui-input-block">
                <input type="text" name="msg_title"  id="msg_title" placeholder="消息标题" autocomplete="off" class="layui-input" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">消息内容</label>
            <div class="layui-input-block">
                <textarea type="text" name="msg_content"  id="msg_content" placeholder="消息内容" autocomplete="off" class="layui-input" lay-verify="required" style="height: 50px"></textarea>
            </div>
        </div>
        <input type="hidden" name="uid" id="uid">

        <div class="layui-form-item layui-layout-admin">
            <div class="layui-input-block">
                <div class="layui-inline" style="left: 0;">
                    <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="editpasssubmit">立即提交</button>
                </div>
            </div>
        </div>
    </form>
</div>
<!---添加批量发送-->
<div id="addmodal"  style="display: none;padding:20px;">
    <form class="layui-form" action="" lay-filter="component-form-group">
        <div class="layui-form-item">
            <label class="layui-form-label">消息标题</label>
            <div class="layui-input-block">
                <input type="text" name="msg_title1"  id="msg_title1" placeholder="消息标题" autocomplete="off" class="layui-input" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">消息内容</label>
            <div class="layui-input-block">
                <textarea type="text" name="msg_content1"  id="msg_content1" placeholder="消息内容" autocomplete="off" class="layui-input" lay-verify="required" style="height: 100px"></textarea>
            </div>
        </div>
        <input type="hidden" name="ids" id="ids">

        <div class="layui-form-item layui-layout-admin">
            <div class="layui-input-block">
                <div class="layui-inline" style="left: 0;">
                    <button class="layui-btn layui-btn-normal"  id="more_send">立即提交</button>
                </div>
            </div>
        </div>
    </form>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','form','table','laydate','set','upload'], function(){
        $ = layui.jquery;
        var table = layui.table
            ,admin = layui.admin
            ,element = layui.element
            ,layer = layui.layer
            ,upload = layui.upload
            ,laydate = layui.laydate
            ,form = layui.form;

        table.render({
            id:'te',
            elem: '#test-table-operate'
            ,url: "{:url('Push/system')}"
            ,height: 'full-155'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'},
                {field:'uid',width:80,align:"center",  title: 'ID'}

                ,{field:'nickname',align:'center',title: '用户昵称'}
                ,{field:'phone',align:'center',title: '用户手机号'}
                ,{width:150, align:'center',width:350, title: '操作',fixed: 'right', toolbar: '#test-table-operate-barDemo'}
            ]]
            ,page: true
            ,limit: 20
        });

        form.render(null, 'component-form-group');

        laydate.render({
            elem: '#LAY-component-form-group-date'
        });

        var ids = [];
        //监听工具条
        table.on('tool(test-table-operate)', function(obj){
            var data = obj.data;

            $("#uid").val(data.uid);
            if(obj.event === 'edit'){
                var addpassmodal = layer.open({
                    title:'添加系统消息'
                    ,type: 1
                    ,shadeClose: true
                    ,area: admin.screen() < 2 ? ['80%'] : ['700px']
                    ,content: $('#addpassmodal')
                });

            }
            //修改
            form.on('submit(editpasssubmit)', function(data){
                var loading = layer.load(1, {
                    shade: [0.1,'#fff'] //0.1透明度的白色背景
                });

                $.ajax({
                    url: "{:url('Push/send')}",
                    type: 'post',
                    dataType: 'text',
                    data: {data:data.field},
                    success: function(info){
                        if (info === "1") {
                            layer.msg('发送成功',{icon:1,time:2000},function(){
                                layer.closeAll();
                                window.location.reload();
                            });
                        }else{
                            layer.close(loading);
                            layer.msg('发送失败',{icon:2,time:1000});
                            window.location.reload();
                        }
                    }
                })
                return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
            });

        });
        $("#send").click(function () {
            var checkStatus = table.checkStatus('te');

            $(checkStatus.data).each(function (i,o) {
                ids.push(o.uid)
            });
            if (ids.length < 1) {
                layer.msg('无选中项');
                return false;
            }else{
                ids = ids.join(",");
                console.log(ids);
                $("#ids").val(ids);
                layer.open({
                    title: '批量发送'
                    , type: 1
                    , shadeClose: true
                    ,area: admin.screen() < 2 ? ['80%'] : ['700px']
                    ,zIndex:10
                    , content: $('#addmodal')
                });
            }

        })
        $("#more_send").click(function () {
            var content = $("#msg_content1").val();
            var title = $("#msg_title1").val();
            if(content == "" ||title==""){
                layui.msg('内容不能为空');
                return false;
            }
            $.ajax({
                url: "{:url('Push/more_send')}",
                type: 'post',
                dataType: 'text',
                data: {id:ids,content:content,title:title},
                success: function(info){
                    if (info === "1") {
                        layer.msg('发送成功',{icon:1,time:2000},function(){
                            layer.closeAll();
                            window.location.reload();
                        });
                    }else{
                        layer.close(loading);
                        layer.msg('发送失败',{icon:2,time:1000});
                        window.location.reload();
                    }
                }
            })
            return false;
        })

        var $ = layui.$, active = {


        };
        //绑定点击事件
        $('.test-table-operate-btn .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });


    });



</script>


</body>
</html>